package dao;

import net.sf.json.JSONObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDao {

    private List<Object> roleDetail(String sql, Object[] params){
        final List<Object> results = new ArrayList<>();
        return Query.executeQueryList(results, sql,params, new QueryCallBackList() {
            @Override
            public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
                try {
                    while (rs.next()) {
                        JSONObject role = new JSONObject();
                        role.put("id",rs.getInt("role_id"));
                        role.put("name",rs.getString("role_name"));
                        role.put("powerCode",rs.getString("role_power_code"));
                        role.put("introduction",rs.getString("role_introduction"));
                        results.add(role);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return results;
            }
        });
    }

    public boolean addRole(String name, String power_code, String introduction) {
        String sql = "insert into `role` (role_name, role_power_code, role_introduction) VALUES (?,?,?)";
        return Query.executeDML(sql, new Object[]{name,power_code,introduction})>0;
    }

    public boolean deleteRole(int id) {
        String sql = "delete from `role` where role_id = ?";
        return Query.executeDML(sql, new Object[]{id})>0;
    }

    public boolean alterRole(int id, String name, String power_code, String introduction) {
        String sql = "update `role` set role_name=?,role_power_code=?, role_introduction=? where role_id=?";
        return Query.executeDML(sql, new Object[]{name, power_code, introduction, id})>0;
    }

    public JSONObject showRoleDetail(int id) {
        String sql = "select * from role where role_id=?";
        JSONObject result = new JSONObject();
        List<Object> results = this.roleDetail(sql, new Object[]{id});
        if(results.size()>0){
            result = JSONObject.fromObject(results.get(0));
        }
        return result;
    }

    public List<Object> showAllRole() {
        String sql = "select * from role order by role_id desc";
        return this.roleDetail(sql, new Object[]{});
    }

    public List<Object> showPagedRole(Integer onePageCount, Integer currentPage) {
        String sql = "select * from role order by role_id desc limit " + (currentPage - 1) * onePageCount + "," + onePageCount;
        return this.roleDetail(sql, new Object[]{});
    }

    public int rolePageNum(Integer onePageCount) {
        int roleNum = this.getRoleNum();
        return (roleNum % onePageCount) == 0 ? (roleNum / onePageCount) : (roleNum / onePageCount + 1);

    }

    private synchronized int getRoleNum() {
        String sql = "select count(*) from role";
        return PublicDao.oneNum(sql, null);
    }
}
